module.exports = function(success, error) {
    if(typeof error !== 'function'){
        error = () => {
          console.log('连接失败~~~');
        }
      }
       //导入 mongoose
       const mongoose = require('mongoose');

       //导入配置文件
       const {DBHOST,DBPORT,DBNAME } = require('../config/config')

       //设置查询模式为严格模式
       mongoose.set('strictQuery', true);
       //连接mongodb
       mongoose.connect(`mongodb://${DBHOST}:${DBPORT}/${DBNAME}`);

       //设置回调
       mongoose.connection.once('open', () => {
        //设置集合中文档的属性以及属性值的类型
        success()
      })
      // 设置连接错误的回调
       mongoose.connection.on('error', () => {
          console.log('连接失败');
      });
    
          //设置连接关闭的回调
       mongoose.connection.on('close', () => {
         console.log('连接关闭');
       });
}